<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * @todo 质检管理
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2016-3-19
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;
class CheckOrderController extends AdminController{
    
    /**
     * @todo    申请核销店铺列表
     */
    public function applyShopList(){
        
        // 活动列表
        $activity_list = D('Activity','Service')->getActivityList(0);
        // 获取申请核销店铺
        $apply_shop = D('CheckOrder')->getApplyShopList();
        //$apply_shop = $this->lists(D('OrderShopView'),$map,'apply_time asc','distinct(iweb_order.shop_id),shop_name,apply_time,merchant_id,activity_id,order_status');
        $this->meta_title = '申请核销店铺';
        $this->assign('activity_list', $activity_list);
        $this->assign('apply_shop', $apply_shop);
        $this->display('apply_shop');
    }
    
    /**
     * @todo    订单质检列表
     */
    public function checkOrderList(){
        
        //获取搜索条件
        $activity_id = (int)I('activity_id');//活动搜索ID号
        $merchant_id = (int)I('merchant_id');//商户搜索ID号
        $shop_id = (int)I('shop_id');//店铺搜索ID号
        $order_type = (int)I('order_type');//订单类型
        $order_status = (int)I('order_status',100);//订单状态
        $order_id = (int)I('order_id');//订单号
        $paper_order = (int)I('paper_order');//纸质订单号
        $phone_number = strip_tags(I('phone_number'));//手机号
        $coupons_type = (int)I('coupons_type', -1);//是否使用优惠卷
        //时间区间
        $apply_start_time = (I('apply_start_time'))? strtotime(I('apply_start_time')) : 0 ;
        $apply_end_time = (I('apply_end_time'))? strtotime(I('apply_end_time')) : NOW_TIME ;
        
        //根据手机号读取用户ID
        if(preg_match('/^1[3-8][0-9]{9}$/', $phone_number)){
            $customer_info = D('Admin/Customer')->getCustomerByMobilePhone($phone_number,'customer_id');
        }
        
        //搜索条件显示
        $activity_list = D('Activity','Service')->getActivityList(0);//活动列表
        if($activity_id != 0){
            $merchant_list = D('Admin/Shop')->getActivityMerchant($activity_id);
        }else{
            $merchant_list = null;
        }
        if($activity_id != 0 && $merchant_id != 0){
            $shop_list = D('Admin/Shop')->getMerchantShop($activity_id,$merchant_id);
        }else{
            $shop_list = null;
        }
         
        $map = array();
        //查询条件
        if($activity_id != 0){
            $map['activity_id'] = $activity_id;
        }
        if($merchant_id != 0){
            $map['merchant_id'] = $merchant_id;
        }
        if($shop_id != 0){
            $map['shop_id'] = $shop_id;
        }
        if($order_type != 0){
            $map['order_type'] = $order_type;
        }
        if($paper_order != 0 ){
            $map['paper_order'] = $paper_order;
        }
        if(!empty($customer_info)){
            $map['customer_id'] = $customer_info['customer_id'];
        }
        if($order_id != 0){
            $map['order_id'] = $order_id;
        }
        if($coupons_type > -1){
            $map['coupons_type'] = $coupons_type;
        }
        if($apply_start_time > 0){
            $map['apply_time'] = array('between', array($apply_start_time, $apply_end_time));
        }
        $map['order_status'] = 5;
        //列表
        $list = null;
        if(!empty($map)){
            //输入手机号不是系统用户时
            if($phone_number && empty($customer_info)){
                $list = null;
            }else{
                $list = $this->lists(D('CheckOrderView'),$map,'order_id desc');
            }
        }
        $this->assign('_list',$list);
    	$this->assign('activity_list',$activity_list);
    	$this->assign('merchant_list',$merchant_list);
    	$this->assign('shop_list',$shop_list);
        $this->assign('meta_title','质检订单列表');
        $this->display('check_order');
    }
    
    /**
     * @todo    订单质检详细
     */
    public function checkOrderInfo(){
        
        $order_id = (int)I('order_id');
        if($order_id == 0){
            $this->error('参数错误');
        }
        $obj = D('CheckOrder');
        $info = $obj->getCheckOrderInfo(array('order_id' => $order_id));
        $visit_list = $obj->getCheckOrderLogs(array('order_id' => $order_id, 'member_id' => UID));
        $this->assign('meta_title','质检订单详细');
        $this->assign('last_url', $_SERVER['HTTP_REFERER']);
        $this->assign('info', $info);
        $this->assign('visit_list', $visit_list);
        $this->display('check_order_info');
    }
    
    /**
     * @todo    订单质检信息更新
     */
    public function checkUpdate(){
        $obj = D('CheckOrder');
        $res = $obj->checkOrderUpdate();
        if($res === false){
            $this->error('操作失败，请稍后再试');
        }
        // 质检日志添加
        M('check_order_log')->add(array('order_id'=>(int)$_POST['order_id'],'member_id'=>UID,'create_time'=>NOW_TIME));
        $this->success('操作成功');
    }
    
    /**
     * @todo 质检店铺列表
     * @author sap
     * @since 2016-3-19
     */
    public function checkShopList(){
        $map = array();
        $map['status']=array('gt',-1);
        $list = $this->lists('CheckShop',$map,'check_shop_id desc','check_shop_id,activity_id,merchant_id,shop_id,is_pass,fail_order,remark,create_time,apply_start,apply_end');
        $this->assign('_list',$list);
        $this->assign('meta_title','质检店铺列表');
        $this->display('shop_list');
    }
    
    /**
     * @todo 添加质检店铺
     * @author sap
     */
    public function checkShopAdd(){
        if(IS_POST){
            $this->checkShopUpdate();
        }else{
            $this->getActivityList();
            $this->assign('list',null);
            $this->assign('meta_title','新增质检店铺');
            $this->assign('meta_title_c','质检店铺列表');
            $this->display('shop_add');
        }
    }
    
    /**
     * @todo 编辑质检店铺
     * @author sap
     */
    public function checkShopEdit(){
        $check_shop_id = (int)I('check_shop_id');
        if($check_shop_id == 0){
            $this->error('请正确操作');
        }
        if(IS_POST){
            $this->checkShopUpdate();
        }else{
            $info = D('Admin/CheckShop')->checkShopShow($check_shop_id);
            $this->getActivityList();
            $obj_shop = D('Admin/Shop');
            $merchant_list = $obj_shop->getActivityMerchant($info['activity_id']);
            $shop_list = $obj_shop->getMerchantShop($info['activity_id'],$info['merchant_id']);
            
            $this->assign('list',$info);
            $this->assign('merchant_list',$merchant_list);
            $this->assign('shop_list',$shop_list);
            
            $this->assign('meta_title','更新质检店铺');
            $this->assign('meta_title_c','质检店铺列表');
            $this->display('shop_add');
        }
    }
    
    /**
     * @todo 添加/编辑质检店铺(仅供内部调用)
     * @author sap
     */
    private function checkShopUpdate(){
        $obj = D('CheckShop');
        $id = $obj->update();
        if(empty($id)){
            $this->error($obj->getError());
        }
        $this->success('操作成功',U('CheckOrder/checkShopList'));
    }
    
    /**
     * @todo 质检店铺状态修改
     * @param string $method 标识
     * @author sap
     */
    public function checkShopChangeStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if(empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map['check_shop_id'] = array('in',$id);
        switch ( strtolower($method) ){
            case 'deletecheckshop':
                $this->delete('CheckShop', $map );
                //记录行为
                action_log('check_shop_del', 'check_shop', $id, UID);
                break;
            default:
                $this->error('参数非法');
        }
    }
    
    /**
     * @todo 统计
     * @author sap
     */
    public function checkStatistics(){
        if(IS_POST){
            $obj = D('CheckShop');
            $info = $obj->checkStatistics();
            $result = array();
            if($info){
                $result['status'] = 1;
                $result['info'] = $info;
            }else{
                $result['status'] = 0;
                $result['info'] = $obj->getError();
            }
            $this->ajaxReturn($result);
        }else{
            $this->getActivityList();
            $this->assign('meta_title','回访统计');
            $this->display('check_statistics');
        }
    }
    
    /**
     * @todo 活动列表
     * @author sap
     */
    private function getActivityList(){
        $activity_list = D('Activity','Service')->getActivityList(0);//活动列表
        $this->assign('activity_list',$activity_list);
    }
    
    
}